<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <input type="text" id="txt" placeholder="输入一个数字">
  <br>
  <div id="show"></div>
  <button id="btn">求和</button>
  <script>
    var txt = document.getElementById("txt");
    var show = document.getElementById("show");
    var btn = document.getElementById("btn")

    btn.onclick = function () {
      var num = txt.value;//获取输入的值
      show.innerHTML = total(num);
    }
    function total(num) {
      var sum = 0;
      for (var i = 0; i <= num; i++) {
        sum += i;
      }
      return sum
    }

  </script>
</body>

</html>